package com.songcha.library_common.ui.dialog

import android.view.Gravity
import android.view.View
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
import com.songcha.library_common.ui.dialog.BaseDialogFragment
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ScreenUtil


abstract class CustomBottomSheetDialog():BaseDialogFragment(){
    protected open lateinit var mBehavior:BottomSheetBehavior<View>

    init {
        setGravity(Gravity.BOTTOM)
        setWidth(ScreenUtil.getScreenWidth())
        setAnim(com.songcha.library_common.R.style.dialogBottomSlideWindowAnim)
    }

    override fun initView(view: View) {
        mBehavior = BottomSheetBehavior.from(getBehaviorView(view))
        //toggleBehavior()

        //first set state cannot receive callback
        mBehavior.addBottomSheetCallback(object : BottomSheetCallback(){
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                LogUtil.log("onStateChanged",newState)
                if(newState == BottomSheetBehavior.STATE_HIDDEN){
                    //mBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
                    dismiss()
                }
                //if(isShowing() && (newState == BottomSheetBehavior.STATE_COLLAPSED || newState == BottomSheetBehavior.STATE_HIDDEN))
                    //dismiss()
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {

            }

        })

        mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    abstract fun getBehaviorView(view:View):View


}